package com.lg.happyfarmer.base;

import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.hemaapp.HemaConfig;
import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.hm_FrameWork.album.ImageLoader;
import com.lg.happyfarmer.activity.CodeVerifyActivity;
import com.lg.happyfarmer.bean.SysInitInfo;
import com.lg.happyfarmer.bean.User;
import com.lg.happyfarmer.db.SysInfoDBHelper;
import com.lg.happyfarmer.db.UserDBHelper;

import xtom.frame.XtomConfig;
import xtom.frame.util.XtomBaseUtil;
import xtom.frame.util.XtomLogger;
import xtom.frame.util.XtomSharedPreferencesUtil;

/**
 * 该项目自定义Application
 */
public class BaseApplication extends EjnhApplication {
    private static final String TAG = BaseApplication.class.getSimpleName();
    private SysInitInfo sysInitInfo;// 系统初始化信息
    private User user;
    private static BaseApplication application;
    private TimeThread timeThread;
    // 屏幕宽度
    private int width;
    private int ten;

    public static int time;
    public static String phone="";
    public static BaseApplication getInstance() {
        return application;
    }

    {
        //微信
//        PlatformConfig.setWeixin("wx6756e87d810f7fd2", "d4624c36b6795d1d99dcf0547af5443d");
        //QQ
//        PlatformConfig.setQQZone("1105294560", "PT51iwm3pDPz4qsX");
        //PlatformConfig.setQQZone("101415826", "e94965e2a192e2942a0d70ff53809b81");
    }

    @Override
    public void onCreate() {
//        UMShareAPI.get(this);
        application = this;
        XtomConfig.LOG = BaseConfig.DEBUG;
        HemaConfig.UMENG_ENABLE = BaseConfig.UMENG_ENABLE;
        XtomConfig.DIGITAL_CHECK = true;
        XtomConfig.DATAKEY = "Dh5f1WtKNPms2Zn";
        String iow = XtomSharedPreferencesUtil.get(this, "imageload_onlywifi");
        XtomConfig.IMAGELOAD_ONLYWIFI = "true".equals(iow);
        XtomLogger.i(TAG, "onCreate");
        super.onCreate();

    }

    @Override
    public void onLowMemory() {
        XtomLogger.i(TAG, "onLowMemory");
        super.onLowMemory();
    }

    @Override
    public void onTerminate() {
        XtomLogger.i(TAG, "onTerminate");
        super.onTerminate();
    }

    /**
     * @return 当前用户
     */
    public User getUser() {
        if (user == null) {
            UserDBHelper helper = new UserDBHelper(this);
            String username = "";
            if (HemaUtil.isThirdSave(getApplicationContext())) {
                username = XtomSharedPreferencesUtil.get(this, "mobile");
            } else {
                username = XtomSharedPreferencesUtil.get(this, "username");
            }
            user = helper.select(username);
            helper.close();
        }
        return user;
    }

    /**
     * 设置保存当前用户
     *
     * @param user 当前用户
     */
    public void setUser(User user) {
        this.user = user;
        UserDBHelper helper = new UserDBHelper(this);
        if (user != null) {
            helper.insertOrUpdate(user);
            helper.close();
        } else {
            helper.clear();
        }
    }

    /**
     * @return 系统初始化信息
     */
    public SysInitInfo getSysInitInfo() {
        if (sysInitInfo == null) {
            SysInfoDBHelper helper = new SysInfoDBHelper(this);
            sysInitInfo = helper.select();
            helper.close();
        }
        return sysInitInfo;
    }

    /**
     * 设置保存系统初始化信息
     *
     * @param sysInitInfo 系统初始化信息
     */
    public void setSysInitInfo(SysInitInfo sysInitInfo) {
        this.sysInitInfo = sysInitInfo;
        if (sysInitInfo != null) {
            SysInfoDBHelper helper = new SysInfoDBHelper(this);
            helper.insertOrUpdate(sysInitInfo);
            helper.close();
        }
    }

    public int getWidth() {
        if (width == 0) {
            width = getResources().getDisplayMetrics().widthPixels;
        }
        return width;
    }

    /**
     * 获取十个相对像素，在当前手机上占的位置
     *
     * @return
     */
    public int getTenDip() {
        if (ten == 0) {
            ten = BaseUtil.dip2px(this, 10);
        }
        return ten;
    }

    private class TimeThread extends Thread {
        private int curr;

        private  TimeHandler timeHandler;

        public TimeThread( TimeHandler timeHandler,int curr) {
            this.timeHandler = timeHandler;
            this.curr = curr;
        }

        void cancel() {
            curr = 0;
        }

        @Override
        public void run() {
            while (curr > 0) {
                timeHandler.sendEmptyMessage(curr);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // ignore
                }
                curr--;
            }
            timeHandler.sendEmptyMessage(-1);
        }
    }


    private static class TimeHandler extends Handler {

        public TimeHandler() {
            

        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case -1:
                    time=0;
                    break;
                default:
                    time=msg.what;

                    break;
            }
        }
    }

    public void startTimer(int time){
        timeThread = new   TimeThread(new   TimeHandler(),time);
        timeThread.start();
    }
}
